import json
from enum import Enum

import requests

"""
    自定义异常枚举
"""


class api_response_code(Enum):
    """
    自定义异常
    """
    CUSTOM_MUST_INPUT = {"code": 10000, "msg": "必填项"}
    INTERFACE_ASSERTION_FAIL = {"code": 20000, "msg": "接口断言失败"}
    NETWORK_ERROR = {"code": 90001, "msg": "网络异常"}

    """
    登录鉴权
    """
    API_LOGIN_ERROR = {"code": 130000, "msg": "登录异常"}
    API_LOGIN_PASSWORD_ERROR = {"code": 130001, "msg": "密码错误"}
    API_LOGIN_USER_NOT_EXIST = {"code": 130002, "msg": "用户不存在"}
    API_AUTH_USER_NOT_TOKEN = {"code": 130003, "msg": "用户未登录"}

    """
    首页模块
    """
    API_INDEX_ERROR = {"code": 140000, "msg": "首页模块"}

    """
    事件消息模块
    """
    API_EVENT_MESSAGE_ERROR = {"code": 150000, "msg": "事件消息模块"}

    """
    设备模块
    """
    API_DEVICE_NOT_EXIST = {"code": 160000, "msg": "设备不存在"}
    API_DEVICE_UPGRADE_ERROR = {"code": 160001, "msg": "设备升级失败"}

    @property
    def getCode(self):
        return self.value["code"]

    @property
    def getMsg(self):
        return self.value["msg"]

    @staticmethod
    def search_msg_by_code(code: int) -> str:
        # 异常信息错误码 地址
        error_code_url = 'http://10.10.0.10/cloud-app/respcode/all'
        response = requests.get(error_code_url)
        responses = response.text.encode('utf-8').decode('utf-8')
        resp_jsons = json.loads(responses).get("data")
        # print(resp_jsons)
        msg = ''
        if resp_jsons:
            for resp_json in resp_jsons:
                if code == resp_json.get('code'):
                    msg = resp_json.get('desc')
        return msg
